﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Se.Model.Achievement
{
    public abstract class AchievementConditionBase
    {
        public AchievementConditionBase(AchievementConditionEnum _type)
        {
            this.Type = _type;
        }

        public long PlayerId;

        /// <summary>
        /// 条件类型
        /// </summary>
        public readonly AchievementConditionEnum Type;
        /// <summary>
        /// 是否完成
        /// </summary>
        public bool IsComplete { get { return this.CurrentCount >= Count; } }

        /// <summary>
        /// 数量
        /// </summary>
        public int Count;

        /// <summary>
        /// 参数1
        /// </summary>
        public string Param1 { get; set; }
        /// <summary>
        /// 参数2
        /// </summary>
        public string Param2 { get; set; }


        private int _currentCount;
        /// <summary>
        /// 成就变化事件
        /// </summary>
        private event Action ConditionChanged;

        /// <summary>
        /// 当前达成数量
        /// </summary>
        public int CurrentCount
        {
            get { return this._currentCount; }
            set
            {
                this._currentCount = value;
                if (this.ConditionChanged != null)
                {
                    this.ConditionChanged();
                }

                if (this._currentCount >= Count)
                {
                    this.StopCondition();
                }
            }
        }


        /// <summary>
        /// 解析成就项的参数
        /// </summary>
        protected abstract void ResolveParams();

        /// <summary>
        /// 注册达成项事件
        /// </summary>
        protected abstract void RegistEvent();
        /// <summary>
        /// 取消注册达成项事件
        /// </summary>
        protected abstract void UnRegistEvent();
        /// <summary>
        /// 检查条件是否完成
        /// </summary>
        protected abstract void CheckCompleted();

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="count"></param>
        /// <param name="param1"></param>
        /// <param name="param2"></param>
        public void Init(int count, int currentCount, string param1, string param2)
        {
            this.Count = count;
            this.CurrentCount = currentCount;
            this.Param1 = param1;
            this.Param2 = param2;
            this.ResolveParams();
        }



        /// <summary>
        /// 开始(current，当前达成的数量）
        /// </summary>
        public void StartCondition(Action countChanged)
        {
            if (this.IsComplete)
            {
                UnRegistEvent();
            }
            else
            {
                RegistEvent();
            }
            this.ConditionChanged += countChanged;
            this.CheckCompleted();
        }

        public void StopCondition()
        {
            UnRegistEvent();            
        }

        /// <summary>
        /// 重启达成项
        /// </summary>
        /// <param name="currentCount"></param>
        public void RestartCondition(int currentCount)
        {
            UnRegistEvent();
            RegistEvent();
        }

        //public abstract bool CheckFinished(object _obj, out string finishProgress);

        //public abstract int GetDefaultProgress();
    }
}
